package com.jinsp.webviewservice.webview;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.util.ArrayMap;
import android.util.Log;

import com.google.gson.Gson;
import com.jinsp.webviewservice.IClientCallback;
import com.jinsp.webviewservice.IWebViewMainService;
import com.jinsp.webviewservice.command.ICommand;

import java.util.HashMap;
import java.util.ServiceLoader;

public class WebViewMainService extends Service {

    private static final String TAG = WebViewMainService.class.getCanonicalName();
    private RemoteCallbackList<IClientCallback> mClientCallbackList;
    private Handler mHandler;

    public WebViewMainService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "创建主进程服务");
        mClientCallbackList = new RemoteCallbackList<>();
        mHandler = new Handler(Looper.getMainLooper());
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new WebViewMainServiceImpl();
    }

    private class WebViewMainServiceImpl extends IWebViewMainService.Stub {

        private final ArrayMap<String, ICommand> commandArrayMap = new ArrayMap<>(32);

        public WebViewMainServiceImpl() {
            //寻找所有实现ICommand的类，配合AutoService实现SPI
            ServiceLoader<ICommand> serviceLoader = ServiceLoader.load(ICommand.class);
            if (serviceLoader != null) {
                for (ICommand command : serviceLoader) {
                    commandArrayMap.put(command.commandName(), command);
                }
            }
        }

        @Override
        public void registerClientCallback(IClientCallback clientCallback) throws RemoteException {
            mClientCallbackList.register(clientCallback);
        }

        @Override
        public void unregisterClientCallback(IClientCallback clientCallback) throws RemoteException {
            mClientCallbackList.unregister(clientCallback);
        }

        @Override
        public void handleWebCommand(String commandName, String jsonData) throws RemoteException {
            mHandler.post(() -> {
                if (commandArrayMap.containsKey(commandName) && mClientCallbackList.getRegisteredCallbackCount() > 0) {
                    mClientCallbackList.beginBroadcast();
                    commandArrayMap.get(commandName).execute(new Gson().fromJson(jsonData, HashMap.class),
                            mClientCallbackList.getBroadcastItem(0));
                    mClientCallbackList.finishBroadcast();
                } else {
                    Log.e(TAG, "未发现注册的命令 " + commandName);
                }
            });
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
        mClientCallbackList.kill();
        mClientCallbackList = null;
        mHandler = null;
    }
}